package com.tuanzi.loan.business.entity.receipt.product;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.persistence.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;

import com.tuanzi.server.loan_base.constant.receipt.LoanReceiptProductComboType;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import com.tuanzi.loan.core.entity.AuditEntity;
import com.tuanzi.server.loan_base.constant.LoanReceiptProductStatus;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@Entity
@Table(name = "LOAN_RECEIPT_PRODUCT_INFO")
@EqualsAndHashCode(callSuper = true)
public class ReceiptProductInfo extends AuditEntity {

	@Id
	@Column(name = "ID")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Length(max = 36)
	@Column(name = "USER_ID")
	private String userId;

	@NotBlank
	@Length(max = 60)
	@Column(name = "NAME")
	private String name;

	@NotBlank
	@Length(max = 60)
	@Column(name = "SUMMARY")
	private String summary;

	@NotBlank
	@Length(max = 255)
	@Column(name = "LOGO")
	private String logo;

	@Length(max = 255)
	@Column(name = "ICON")
	private String icon;

	@NotNull
	@Max(Integer.MAX_VALUE)
	@Column(name = "MIN_LOAN")
	private Integer minLoan;

	@NotNull
	@Max(Integer.MAX_VALUE)
	@Column(name = "MAX_LOAN")
	private Integer maxLoan;

	@NotNull
	@Max(Integer.MAX_VALUE)
	@Column(name = "MIN_TERM")
	private Integer minTerm;

	@NotNull
	@Max(Integer.MAX_VALUE)
	@Column(name = "MAX_TERM")
	private Integer maxTerm;

	@NotNull
	@Column(name = "MIN_RATE")
	private BigDecimal minRate;

	@NotNull
	@Column(name = "MAX_RATE")
	private BigDecimal maxRate;

	@NotNull
	@Column(name = "UNIT_PRICE")
	private BigDecimal unitPrice;

	@NotNull
	@Column(name = "LIMIT_ORDER_DAY")
	private Integer limitOrderOfDay;

	@Column(name = "LIMIT_BEGIN_DAY")
	private Integer limitBeginDay;

	@Column(name = "LIMIT_END_DAY")
	private Integer limitEndDay;

	@Column(name = "LIMIT_WEEK")
	private String limitWeek;

	@Column(name = "COMBO_TYPE")
	@NotNull
	private Integer comboType;

	@Column(name = "OPERATION_PERMISSION")
	private Boolean operationPermission; //运营商授权

	@Column(name = "SESAME_ATTESTATION")
	private Boolean sesameAttestation; //芝麻分认证

	@NotNull
	@Column(name = "STATUS")
	private LoanReceiptProductStatus status;

	@Length(max = 255)
	@Column(name = "LABEL")
	private String label;

	@Length(max = 10)
	@Column(name = "PLATFORM")
	private String platform;

	@Column(name = "ORDER_ALLOT_TYPE")
	private Integer orderAllotType;

	@Column(name = "SORT")
	private Integer sort;

	@Length(max = 255)
	@Column(name = "REJECT_REASON")
	private String rejectReason;

	@Column(name = "JOIN_ONE_KEY")
	private Boolean joinOneKey;

	@Transient
	private Date limitEndHour;

	@Transient
	private Date limitBeginHour;

	@Transient
	private List<String> weeks;

	@Transient
	private List<String> labels;

	public Integer getLimitBeginDay() {
		if (limitBeginHour != null) {
			limitBeginDay = new DateTime(limitBeginHour).getSecondOfDay();
		}
		return limitBeginDay;
	}

	public void setLimitBeginDay(Integer limitBeginDay) {
		if (limitBeginDay != null) {
			limitBeginHour = new DateTime(new LocalDate().toDate()).plusSeconds(limitBeginDay).toDate();
		} else {
			limitBeginHour = null;
		}
		this.limitBeginDay = limitBeginDay;
	}

	public Integer getLimitEndDay() {
		if (limitEndHour != null) {
			limitEndDay = new DateTime(limitEndHour).getSecondOfDay();
		}
		return limitEndDay;
	}

	public void setLimitEndDay(Integer limitEndDay) {
		if (limitEndDay != null) {
			limitEndHour = new DateTime(new LocalDate().toDate()).plusSeconds(limitEndDay).toDate();
		} else {
			limitEndHour = null;
		}
		this.limitEndDay = limitEndDay;
	}

	public String getLabel() {
		if (CollectionUtils.isNotEmpty(labels)) {
			label = labels.stream().collect(Collectors.joining(","));
		}
		return label;
	}

	public String getLimitWeek() {
		if (CollectionUtils.isNotEmpty(weeks)) {
			limitWeek = weeks.stream().collect(Collectors.joining(","));
		}
		return limitWeek;
	}

	public void setLimitWeek(String limitWeek) {
		if (StringUtils.isNotBlank(limitWeek)) {
			weeks = Stream.of(StringUtils.split(limitWeek, ",")).collect(Collectors.toList());
		} else {
			weeks = null;
		}
		this.limitWeek = limitWeek;
	}

	public Date getLimitEndHour() {
		if (limitEndDay != null) {
			limitEndHour = new DateTime(new LocalDate().toDate()).plusSeconds(limitEndDay).toDate();
		}
		return limitEndHour;
	}

	public void setLimitEndHour(Date limitEndHour) {
		if (limitEndHour != null) {
			limitEndDay = new DateTime(limitEndHour).getSecondOfDay();
		} else {
			limitEndDay = null;
		}
		this.limitEndHour = limitEndHour;
	}

	public Date getLimitBeginHour() {
		if (limitBeginDay != null) {
			limitBeginHour = new DateTime(new LocalDate().toDate()).plusSeconds(limitBeginDay).toDate();
		}
		return limitBeginHour;
	}

	public void setLimitBeginHour(Date limitBeginHour) {
		if (limitBeginHour != null) {
			limitBeginDay = new DateTime(limitBeginHour).getSecondOfDay();
		} else {
			limitBeginDay = null;
		}
		this.limitBeginHour = limitBeginHour;
	}

	public List<String> getWeeks() {
		if (StringUtils.isNotBlank(limitWeek)) {
			weeks = Stream.of(StringUtils.split(limitWeek, ",")).collect(Collectors.toList());
		}
		return weeks;
	}

	public void setWeeks(List<String> weeks) {
		if (CollectionUtils.isNotEmpty(weeks)) {
			limitWeek = weeks.stream().collect(Collectors.joining(","));
		} else {
			limitWeek = null;
		}
		this.weeks = weeks;
	}

	public List<String> getLabels() {
		if (StringUtils.isNotBlank(label)) {
			labels = Stream.of(StringUtils.split(label, ",")).collect(Collectors.toList());
		}
		return labels;
	}

	public void setLabels(List<String> labels) {
		if (CollectionUtils.isNotEmpty(labels)) {
			label = labels.stream().collect(Collectors.joining(","));
		} else {
			label = null;
		}
		this.labels = labels;
	}

	public boolean isComboOne(){
		return comboType == LoanReceiptProductComboType.USER_PHONERECORD_PERMISSION.getType();
	}

	public boolean isComboTwo(){
		return comboType == LoanReceiptProductComboType.USER_PERMISSION.getType();
	}

	public boolean isComboThree(){
		return comboType == LoanReceiptProductComboType.USER_PHONERECORE_SESAME_PERMISSION.getType();
	}
}
